﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Castle.Core.Logging;

namespace TVN.ShopMan.ApplicationInterface.AppForms
{
    using TVN.Common.Interfaces;
    using TVN.ShopMan.ActiveModels;
    using TVN.ShopMan.ApplicationInterface.Helpers;

    public partial class SupplierFormEdit : Form
    {
        private static readonly ILogger _logger = Program.Logger.CreateChildLogger("SupplierFormEdit");
        private IRepository<Supplier> supplierRepository = null;
        private IRepository<ContactPoint> contactPointRepository = null;
        private Supplier selectedSupplier = null;

        public Supplier SelectedSupplier
        {
            get { return this.selectedSupplier; }
            set { this.selectedSupplier = value; }
        }

        public SupplierFormEdit()
        {
            InitializeComponent();

            this.SetUp();
            this.LoadData();
        }

        public SupplierFormEdit(Supplier supplier)
        {
            InitializeComponent();

            this.selectedSupplier = supplier;
            this.SetUp();
            this.LoadData();
        }

        private void SetUp()
        {
            this.supplierRepository = Program.Container.Resolve<IRepository<Supplier>>();
            this.contactPointRepository = Program.Container.Resolve<IRepository<ContactPoint>>();
        }

        private void LoadData()
        {
            if (this.selectedSupplier != null)
            {
                this.txtUnitName.Text = this.selectedSupplier.Name;
                this.txtAddress.Text = this.selectedSupplier.Address;
                this.ckbActive.Checked = this.selectedSupplier.IsActive;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool isCreating = ((this.selectedSupplier == null) || (this.selectedSupplier.Id <= 0));
            if (AppHelper<Supplier>.IsExists(Unit.Properties.Name, this.txtUnitName.Name, isCreating))
            {
                this.txtUnitName.Focus();
            }
            else
            {
                this.AssignSupplierData();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void AssignSupplierData()
        {
            try
            {
                #region Save supplier information

                if (this.selectedSupplier == null)
                {
                    this.selectedSupplier = new Supplier
                    {
                        CreatedDate = DateTime.Now,
                        Creator = Program.SessionData.ActiveAccount,
                    };
                }
                else
                {
                    this.selectedSupplier.LastModifiedDate = DateTime.Now;
                    this.selectedSupplier.LastModifier = Program.SessionData.ActiveAccount;
                }

                this.selectedSupplier.Address = this.txtAddress.Text.Trim();
                this.selectedSupplier.IsActive = this.ckbActive.Checked;
                this.selectedSupplier.Name = this.txtUnitName.Text.Trim();

                #endregion

                #region Save contacts

                foreach (var localItem in this.selectedSupplier.SupplierContactPointsSupplierId)
                {
                    if (localItem.ContactPoint.Id > 0)
                    {
                    }
                    else
                    {

                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                _logger.Error("Could not save supplier", ex);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

        }
    }
}
